package com.speakglobe.panelist.dao;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.speakglobe.panelist.model.EWallet;
import com.speakglobe.panelist.model.Panelist;
import com.speakglobe.panelist.model.PanelistUtility;
import com.speakglobe.security.model.User;

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class PanelistDaoImpl implements PanelistDao {
	
	private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean savePanelistUser(User panelistUser) {
		boolean isCreated = true;
		
		try{
			hibernateTemplate.persist(panelistUser);
		}catch(Exception ex){
			isCreated = false;
		}
		
		return isCreated;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean savePanelist(Panelist panelist) {
		boolean isCreated = true;
		
		try{
			hibernateTemplate.persist(panelist);
		}catch(Exception ex){
			isCreated = false;
		}
		
		return isCreated;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean savePanelistUserUtility(
			PanelistUtility panelistUtility) {
		boolean isCreated = true;
		
		try{
			hibernateTemplate.persist(panelistUtility);
		}catch(Exception ex){
			isCreated = false;
		}
		
		return isCreated;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean saveEWallet(EWallet eWallet) {
		boolean isCreated = true;
		
		try{
			hibernateTemplate.persist(eWallet);
		}catch(Exception ex){
			isCreated = false;
		}
		
		return isCreated;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public User updatePanelistUser(User user) {
		
		if(user != null){
			return (User) hibernateTemplate.merge(user);
		} else {
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public Panelist updatePanelist(Panelist panelist) {
		if(panelist != null){
			return (Panelist) hibernateTemplate.merge(panelist);
		} else {
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public PanelistUtility updatePanelistUserUtility(
			PanelistUtility panelistUtility) {
		if(panelistUtility != null){
			return (PanelistUtility) hibernateTemplate.merge(panelistUtility);
		} else {
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public EWallet updateEWallet(EWallet eWallet) {
		if(eWallet != null){
			return (EWallet) hibernateTemplate.merge(eWallet);
		} else {
			return null;
		}
	}

	@Override
	public User findPanelistUserById(int panelistUserId) {
		return (User) hibernateTemplate.get(User.class, panelistUserId);
	}

	@Override
	public Panelist findPanelistById(int panelistId) {
		return (Panelist) hibernateTemplate.get(Panelist.class, panelistId);
	}

	@Override
	public PanelistUtility findPanelistUserUtilityById(int panelistUserUtilityId) {
		return (PanelistUtility) hibernateTemplate.get(PanelistUtility.class, panelistUserUtilityId);
	}

	@Override
	public EWallet findEWalletById(int eWalletId) {
		return (EWallet) hibernateTemplate.get(EWallet.class, eWalletId);
	}

	@Override
	public List<User> findPanelistUsers() {
		return null;
	}

	@Override
	public List<Panelist> findPanelists() {
		return hibernateTemplate.find("from Panelist");
	}

	@Override
	public List<PanelistUtility> findPanelistUserUtilities() {
		return hibernateTemplate.find("from PanelistUserUtility");
	}

	@Override
	public List<EWallet> findEWallets() {
		return hibernateTemplate.find("from EWallet");
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean deletePanelistUser(int panelistUserId) {
		boolean isDeleted = true;
		
		try{
			hibernateTemplate.delete(hibernateTemplate.get(User.class, panelistUserId));
		}catch(Exception ex){
			isDeleted = false;
		}
		
		return isDeleted;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean deletePanelist(int panelistId) {
		boolean isDeleted = true;
		
		try{
			hibernateTemplate.delete(hibernateTemplate.get(Panelist.class, panelistId));
		}catch(Exception ex){
			isDeleted = false;
		}
		
		return isDeleted;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean deletePanelistUserUtility(
			int panelistUserUtilityId) {
		boolean isDeleted = true;
		
		try{
			hibernateTemplate.delete(hibernateTemplate.get(PanelistUtility.class, panelistUserUtilityId));
		}catch(Exception ex){
			isDeleted = false;
		}
		
		return isDeleted;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean deleteEWallet(int eWalletId) {
		boolean isDeleted = true;
		
		try{
			hibernateTemplate.delete(hibernateTemplate.get(EWallet.class, eWalletId));
		}catch(Exception ex){
			isDeleted = false;
		}
		
		return isDeleted;
	}

	@Override
	public Panelist findPanelistByKeyword(String keyword) {
		Object[] paramArr = new Object[1];
		paramArr[0] = new String(keyword);
		
		try{
			return (Panelist)hibernateTemplate.find("from Panelist where username = ?", paramArr).get(0);
		}catch(IndexOutOfBoundsException ex){
			return null;
		}
	}

}
